package lab_16.com.acme.bankapp.service.bank;

import java.util.*;


import lab_16.com.acme.bankapp.domain.bank.*;

public class BankReport {
	/**
	 * Get number of clients in the bank
	 * 
	 * @param bank
	 * @return
	 */
	public static int getNbOfCustomers(Bank bank) {
		return bank.getClients().size();
	}

	/**
	 * Get the overall number of accounts of clients in the bank.
	 * 
	 * @param bank
	 * @return
	 */

	public static Set<Account> getAllAccounts(Bank bank) {
		Set<Account> accounts = new HashSet<Account>();
		for (Client client : bank.getClients()) {
			accounts.addAll(client.getAccounts());
		}
		return Collections.unmodifiableSet(accounts);
	}

	public static int getNbOfAccounts(Bank bank) {
		return getAllAccounts(bank).size();
	}

	static Comparator<Account> AccountSumComparator = new Comparator<Account>() {

		@Override
		public int compare(Account o1, Account o2) {
			return ((Double)o1.getBalance()).compareTo(o2.getBalance());
		}

	};

	public static List<Account> getAccountsSortedBySum(Bank bank) {
		List<Account> accounts = new ArrayList<Account>(getAllAccounts(bank));
		Collections.sort(accounts,AccountSumComparator);
		return accounts;
	}
	
	public static double getBankCredit(Bank bank) {
		double result = 0;
		for (Account account: getAllAccounts(bank)) { {
				if (account.getBalance() < 0) {
					result += account.getBalance();
				}
			}
		}
		return -result;
	}
	
	public static Map<Client,Set<Account>> getCustomerAccount(Bank bank) {
		Map<Client,Set<Account>> map = new HashMap<Client,Set<Account>> ();
		for (Client client: bank.getClients()) {
			map.put(client, client.getAccounts());
		}
		return Collections.unmodifiableMap(map);
	}

}
